import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'girl.dart';

main() =>
    runApp(MaterialApp(debugShowCheckedModeBanner: false, home: HomePage()));

class HomePage extends StatefulWidget {
  @override
  createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  build(context) => Scaffold(
        body: FutureBuilder<GirlModel>(
          future: fetchData(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data.newslist.length ?? 0,
                itemBuilder: (BuildContext context, int index) {
                  final item = snapshot.data.newslist[index];
                  return Column(
                    children: [
                      Text(item.ctime),
                      Container(
                        child: CachedNetworkImage(
                          placeholder: (context, index) =>
                              CircularProgressIndicator(),
                          imageUrl: item.picUrl,
                        ),
                      )
                    ],
                  );
                },
              );
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }
            return Center(child: CircularProgressIndicator());
          },
        ),
      );
  Future<GirlModel> fetchData() async {
    final response = await Dio().get(
        'https://api.tianapi.com/meinv/?key=d4f074e5a8866ff604bd1ee6b981eb96&num=50');
    if (response.statusCode == 200) {
      return GirlModel.fromJson(response.data);
    } else {
      throw Exception();
    }
  }
}
